// ignore_for_file: use_build_context_synchronously, library_private_types_in_public_api

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sp_util/sp_util.dart';
import 'package:storage/pages/all_company_page.dart';
import 'package:storage/pages/all_goods_page.dart';
import 'package:storage/pages/warehouse_in_page.dart';
import 'package:storage/pages/warehouse_out_page.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  String path = '';

  bool needPage = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      Directory documentsDirectory = await getApplicationDocumentsDirectory();
      path = documentsDirectory.path;
      needPage = SpUtil.getBool('needPage') ?? false;
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: const EdgeInsets.all(24),
        child: Row(
          children: [
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  FilledButton(
                      onPressed: () {
                        /// 跳转库存明细页
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const AllGoodsPage()),
                        );
                      },
                      child: const Text('库存明细')),
                  const SizedBox(height: 20),
                  FilledButton(
                      onPressed: () {
                        /// 跳转添加商品页
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const WarehouseInPage()),
                        );
                      },
                      child: const Text('入库记录')),
                  const SizedBox(height: 20),
                  FilledButton(
                      onPressed: () {
                        /// 跳转添加商品页
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const WarehouseOutPage()),
                        );
                      },
                      child: const Text('出库记录')),
                  const SizedBox(height: 20),
                  FilledButton(
                      onPressed: () {
                        /// 跳转公司列表页
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => const AllCompanyPage()),
                        );
                      },
                      child: const Text('公司列表')),
                  const SizedBox(height: 20),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text(
                        '表格是否需要分页',
                      ),
                      Checkbox(
                          value: needPage,
                          onChanged: (value) async {
                            needPage = value ?? false;
                            await SpUtil.putBool('needPage', needPage);
                            setState(() {});
                          })
                    ],
                  )
                ],
              ),
            ),
            Container(
              width: 300,
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                color: Colors.lightBlueAccent,
                borderRadius: BorderRadius.circular(15),
              ),
              child: Text(
                  '由于该系统为本地仓储管理系统，数据未经网络保存至云端，请仔细保管好$path该文件夹下的内容，此处存储了本地的数据库，如有丢失将无法恢复。若使用者希望在另一台电脑上共用本电脑本地的数据库数据，请联系管理员进行迁移。'),
            )
          ],
        ),
      ),
    );
  }
}
